/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.ourgrid.common.interfaces.DiscoveryService;
import org.ourgrid.common.interfaces.RemoteWorkerProvider;
import org.ourgrid.peer.controller.ds.DiscoveryServiceData;

import br.edu.ufcg.lsd.commune.Application;
import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;
import br.edu.ufcg.lsd.commune.container.servicemanager.dao.DAO;
import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.identification.ServiceID;

public class DiscoveryServiceClientDAO extends DAO {
	
	private static int MAX_NUMBER_OF_DS = 10;

	private static int MAX_NUMBER_OF_ALIVE_DS = 2;
	
	private Map<DeploymentID, RemoteWorkerProvider> providers;
	
	private List<DiscoveryServiceData> discoveryServices;

	private Future<?> advertActionFuture;
	
	private final LinkedList<ServiceID> dsIds = new LinkedList<ServiceID>();
	
	/**
	 * @param component
	 */
	public DiscoveryServiceClientDAO(Application application) {
		super(application);
		this.providers = new HashMap<DeploymentID, RemoteWorkerProvider>();
		this.discoveryServices = new ArrayList<DiscoveryServiceData>();
	}

	public DiscoveryService getDiscoveryService() {
		if (IsAliveDsListEmpty()) {
			return null;
		}
		return discoveryServices.get(0).getDiscoveryService();
	}

	public boolean IsAliveDsListEmpty() {
		return discoveryServices.isEmpty();
	}
	
	/**
	 * 
	 * @param discoveryService
	 * @return true if the discovery service list is full, false otherwise
	 */
	public boolean addDiscoveryService(DiscoveryService discoveryService, ServiceID serviceID) {
		discoveryServices.add(new DiscoveryServiceData(serviceID, discoveryService));
		return discoveryServices.size() >= MAX_NUMBER_OF_ALIVE_DS;
	}
	
	public Future<?> getAdvertActionFuture() {
		return advertActionFuture;
	}
	
	public void setAdvertActionFuture(Future<?> advertActionFuture) {
		this.advertActionFuture = advertActionFuture;
	}
	
	public Collection<RemoteWorkerProvider> getRemoteWorkerProviders() {
		return this.providers.values();
	}
	
	public void addRemoteWorkersProvider(DeploymentID deploymentID, RemoteWorkerProvider provider) {
		this.providers.put(deploymentID, provider);
	}

	public RemoteWorkerProvider removeRemoteWorkersProvider(DeploymentID deploymentID) {
		return this.providers.remove(deploymentID);
	}

	public void clear() {
		this.providers.clear();
		
	}

	public boolean isConnected() {
		return discoveryServices != null && !discoveryServices.isEmpty();
	}
	
	/**
	 * Used for tests.
	 * @return
	 */
	public Map<DeploymentID, RemoteWorkerProvider> getProviders() {
		return providers;
	}
	
	public DeploymentID getProviderDeploymentID(ServiceID providerServiceID) {
		
		DeploymentID providerDeploymentID = null;
		
		for (DeploymentID deploymentID : providers.keySet()) {
			
			if (deploymentID.getServiceID().equals(providerServiceID)) {
				providerDeploymentID = deploymentID;
				break;
			}
		}
		
		return providerDeploymentID;
	}

	/**
	 * @return the dsAdresses
	 */
	public List<ServiceID> getDsIDs() {
		return new ArrayList<ServiceID>(dsIds);
	}
	
	public boolean addDsID(ServiceID dsId) {
		boolean changed = false;
		if (!dsIds.contains(dsId)){
			dsIds.addFirst(dsId);
			if(dsIds.size() > MAX_NUMBER_OF_DS){
				dsIds.removeLast();
			}
			changed = true;
		}
		return changed;
	}

	public void removeDiscoveryService( ServiceID serviceID, ServiceManager serviceManager) {
		
		Iterator<DiscoveryServiceData> iterator = discoveryServices.iterator();
		while (iterator.hasNext()) {
			DiscoveryServiceData discoveryServiceData = iterator.next();
			if (serviceID.equals(discoveryServiceData.getServiceID())) {
				iterator.remove();
				break;
			}
		}
		
	}

	public boolean isDsAlive( ServiceID dsId ) {
		for ( DiscoveryServiceData discoveryServiceData : discoveryServices ) {
			if (discoveryServiceData.getServiceID().equals( dsId )) {
				return true;
			}
		}
		return false;
	}
	
}
